package io.github.hadyang.leetcode.offer;

import java.util.Arrays;
import org.junit.Test;

/** @author haoyang.shi */
public class IsContinuous {

  @Test
  public void test() {
    //        System.out.println(isContinuous(new int[]{1, 3, 0, 0, 5}));
    System.out.println(isContinuous(new int[] {1, 3, 2, 6, 4}));
  }

  public boolean isContinuous(int[] numbers) {
    if (numbers == null || numbers.length < 5) return false;

    Arrays.sort(numbers);

    int count = 0;
    int zeroCount = 0;
    int pre = -1;
    for (int number : numbers) {
      if (number == 0) {
        zeroCount++;
        continue;
      }

      if (pre == -1) pre = number;
      else {
        int t = number - pre - 1;
        if (t > 0) {
          count += t;
        } else if (t < 0) return false;

        pre = number;
      }
    }

    if (count == 0) return true;
    else return count == zeroCount;
  }
}
